<?php

namespace App\Admin\Forms;

use App\Models\WechatReplyConfig;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class WechatZdReply extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '被关注回复';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        //dump($request->all());
        WechatReplyConfig::query()->updateOrCreate([
            'type' => 2,
        ], [
            'content' => $request->get('content'),
        ]);
        admin_success('保存内容成功');

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->hidden('title')->value($this->title);
//        $this->editor('content', __('回复内容'))->rules('required', [
//            'required' => '请填写内容'
//        ]);
        $this->textarea('content', __('回复内容'))->rules('required', [
            'required' => '请填写内容'
        ]);
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $content = WechatReplyConfig::query()->where('type', 2)->value('content');
        return [
            'title'   => $this->title,
            'content' => $content,
        ];
    }
}
